package com.example.diary;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.data.DBManger;
import com.example.timecalculous.R;
import com.example.data.TimeData;

import java.util.Calendar;

public class Diary extends Activity {
    Button btn1,btn2;
    TimeData timeData;
    DBManger dbManger;
    Calendar calendar=Calendar.getInstance();
    int year,month,day;
    String year1=null,month1=null,day1=null;
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-genediaryData=new DiaryData(this);rated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.diaryedit);
        final EditText editText= (EditText) findViewById(R.id.edittext);
        timeData=new TimeData(this);
        //设置当前时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        year=calendar.get(Calendar.YEAR);
        month=calendar.get(Calendar.MONTH);
        day=calendar.get(Calendar.DAY_OF_MONTH);
        year1=String.valueOf(year);
        month1=String.valueOf(month);
        day1=String.valueOf(day);
        btn1= (Button) findViewById(R.id.button222);
        btn2= (Button) findViewById(R.id.diarybutton1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=editText.getText().toString();
                insert1(year1,month1,day1,str);
                Toast.makeText(Diary.this, "插入成功 ",
                        Toast.LENGTH_SHORT).show();
                timeData.close();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(Diary.this,DiaryHistories.class);
                startActivity(intent);
            }
        });
    }
    //插入操作
    public long insert1(String year,String month,String day,String textdata)
    {
        dbManger=new DBManger();
        SQLiteDatabase db = timeData.getWritableDatabase();
		/* ContentValues */
        ContentValues cv = new ContentValues();
        cv.put(dbManger.YEAR, year);
        cv.put(dbManger.MONTH, month);
        cv.put(dbManger.DAY, day);
        cv.put(dbManger.DIARY,textdata);
        long row = db.insert(dbManger.TABLE_NAME1, null, cv);
        return row;
    }
}
